<?php

namespace App\Http\Controllers\Admin;

use App\Handlers\ImageUploadHandler;
use App\Http\Requests\BannerRequest;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Banner;
class BannerController extends Controller
{
    public $class = 'yu-banner';
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $class = 'yu-banner';
        $class_1 = 'yu-banner';
//        $lists = Banner::paginate(15);
        $lists = Banner::all();
        return view('admin/banner/index',compact('lists','class','class_1'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $class = 'yu-banner';
        $class_1 = 'yu-banner';
        return view('admin/banner/create',compact('class','class_1'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(BannerRequest $request,ImageUploadHandler $uploader,Banner $banner)
    {
        $data = $request->all();
        if($request->image){
            $result = $uploader->save($request->image,'banner');
            if($result){
                $data['image'] = $result['path'];
            }
        }
        if($banner->storeData($data)){
            return redirect('admin/banner/index')->with('success','新增成功');
        }else{
            return redirect('admin/banner/index')->with('danger','新增失败');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $class = 'yu-banner';
        $class_1 = 'yu-banner';
        $banner = Banner::find($id);
        return view('admin/banner/create',compact('class','banner','class_1'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(BannerRequest $request,ImageUploadHandler $uploader,Banner $banner, $id)
    {
        $data = $request->except('_token');
        if($request->image){
            $result = $uploader->save($request->image,'banner');
            if($result){
                $data['image'] = $result['path'];
            }
        }
        if($banner->updateData($id,$data)){
            return redirect('admin/banner/index')->with('success','更新成功');
        }else{
            return redirect('admin/banner/index')->with('danger','更新失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id,Banner $banner)
    {
        $tempId = intval($id,0);
        if($tempId>0 && $banner->destroyData($tempId)){
            return redirect()->back()->with('success','删除成功');
        }else{
            return redirect()->back()->with('danger','删除失败');
        }

    }
}
